/* global inputSelLoadingHtml, currPage */

function addPigeon() {
    doGetAddPigeonForm(function (res) {
        if (res.Code != 10000) {
            $.toast(res.Message);
            return false;
        }
        var html = res.Result;
        var idx = showDialog(html);
        initForm(idx);
    });

    /**
     * 初始化表单
     * @param {type} idx
     * @returns {undefined}
     */
    function initForm(idx) {
        //初始化生日控件
        $('#birthday').datetimepicker({
            format: 'Y-m-d',
            timepicker: false
        });
        /**
         * 父母双亲搜索输入下拉初始化
         */
        $("#fatherAddInputSel,#motherAddInputSel").inputSelect({
            "isInputEnabled": true,
            "isFilter": false,
            "defaultText": "",
            "onInputBlur": function (t) {
                //失去焦点后判断是不是选择的结果，不是清空
                setTimeout(function () {
                    var tname = $(t).attr("data-tname");
                    var val = $("#addForm input[name=" + tname + "]").val();
                    if (!val) {
                        $(t).val("");
                    }
                }, 200);
            }
        }, "", function (t, val, key) {
            var tname = $(t).find("input").attr("data-tname");
            $("#addForm input[name=" + tname + "]").val(key);
        });

        /**
         * 搜索父母信鸽列表
         * @type type
         */
        var inputOrgVal = {};
        $("#fatherAddInputSel,#motherAddInputSel").on("input", "input[name=search]", function () {
            var tname = $(this).attr("data-tname");
            $("#addForm input[name=" + tname + "]").val("");

            var keyword = $.trim($(this).val());
            var sex = $(this).attr("data-sex");

            //相同的值不再重复搜索
            if (inputOrgVal[tname] == keyword) {
                false;
            }

            var searchData = {
                "keyword": keyword,
                "sex": sex
            };

            var $ul = $(this).parents('.cus-sel').find("ul");
            $ul.html(inputSelLoadingHtml);

            doSearchParentList(searchData, function (res) {

                if (res.Code != 10000) {
                    $.toast(res.Message);
                    return false;
                }

                var html = res.Result.html;
                $ul.html(html);
                inputOrgVal[tname] = keyword;
            }, false);

        });


        /**
         * 羽色眼睛搜索输入下拉初始化
         */
        $("#colorInputSel,#eyeInputSel").inputSelect({
            "isInputEnabled": true,
            "defaultText": ""
        }, "", function (t, val, key) {
            var tname = $(t).find("input").attr("data-tname");
            $("#addForm input[name=" + tname + "]").val(key);
        });
        
        /**
         * 羽色眼睛搜索输入
         */
        $("#colorInputSel,#eyeInputSel").on("input", "input", function () {
            var tname = $(this).attr("data-tname");
            $("#addForm input[name=" + tname + "]").val("");
        });

        $("#colorInputSel,#eyeInputSel").on("focus", "input", function () {
            $(this).siblings(".jselect-icon").trigger("click");
        });

        
        /**
         * 关闭弹窗
         */
        $("#addForm").on("click", ".close-btn", function () {
            try {
                if (idx) {
                    idx.close();
                }
            } catch (e) {
            }
        });

        /**
         * 提交添加
         */
        $("#addForm").on("click", ".submit-btn", function () {
            var data = getFormData($("#addForm"));
            doAddPigeon(data, function (res) {
                if (res.Code != 10000) {
                    $.toast(res.Message);
                    return false;
                }
                try {
                    if (idx) {
                        idx.close();
                    }
                } catch (e) {
                }
                $.toast("添加成功",500,function(){
                    getPigeonList(currPage);
                });
            });
        });
    }

    /**
     * 显示弹窗
     * @param {type} html
     * @returns {unresolved}
     */
    function showDialog(html) {
        return $.alert(html, {
            "lock": true,
            "width": '100%',
            "height": '100%',
            "left": '0%',
            "top": '0%',
            "fixed": true,
            "resize": false,
            "drag": false,
            "title": '新增信鸽',
            "ok": false,
            "overflow": true
        });
    }
}